using System;
using System.Collections.Generic;
using Subsync.Core.Sync;

namespace Subsync.Core.SvnServe
{
	/// <summary>
	/// Author: Steven Goh Jian Wen
	/// </summary>
	public class CLIManager : App
	{
		public override Dictionary<string, List<object>> GetTokens()
        {
            Dictionary<string, List<object>> ret = new Dictionary<string, List<object>>();

            ret.Add("svnserve", new List<object>() { "ss", "svnserve" });
			ret.Add("install_service", new List<object>() { "installservice", "is" });
			ret.Add("uninstall_service", new List<object>() { "uninstallservice", "us" });
			ret.Add("is_active", new List<object>() { "active" });

            return ret;
        }

        public override List<KeyValuePair<string, initPointer>> GetInitPointers ()
        {
        	List<KeyValuePair<string, initPointer>> ret = new List<KeyValuePair<string, initPointer>> ();

            ret.Add (new KeyValuePair<string, initPointer> ("svnserve", new initPointer (this.ToggleSvnServe)));
        	ret.Add (new KeyValuePair<string, initPointer> ("install_service", new initPointer (this.InstallService)));
        	ret.Add (new KeyValuePair<string, initPointer> ("uninstall_service", new initPointer (this.UninstallService)));
			ret.Add (new KeyValuePair<string, initPointer> ("is_active", new initPointer (this.IsActive)));

            return ret;
        }
		
		public void ToggleSvnServe (Dictionary<string, List<object>> tokens)
		{
			SvnServe svnserver = new SvnServe ();
			try {
				svnserver.Execute ();
				if (svnserver.IsActive ())
					Console.WriteLine ("SVNServe is active.");
				else
					Console.WriteLine ("SVNServe is disabled.");
			} catch (UnableToStartSVNServe) {
				Console.WriteLine ("Unable to start SVNServe service.");
			}
		}
		
		public void InstallService (Dictionary<string, List<object>> tokens)
		{
			SvnServe svnserver = new SvnServe ();
			svnserver.InstallWindowsService (new Config().GetProperty("SVNServePath"));
			Console.WriteLine ("Installed.");
		}
		
		public void UninstallService (Dictionary<string, List<object>> tokens)
		{
			SvnServe svnserver = new SvnServe ();
			svnserver.UninstallWindowsService ();
			Console.WriteLine ("Uninstalled.");
		}
		
		public void IsActive (Dictionary<string, List<object>> tokens)
		{
			SvnServe svnserver = new SvnServe ();
			if (svnserver.IsActive ())
				Console.WriteLine ("Active.");
			else Console.WriteLine ("Not active.");
		}
	}
}
